package bb.lanxing.net.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Interceptor;


public class ApiClientConfig {
    private String baseUrl;
    private String cacheDir;
    private long cacheSize;
    private long connectTimeout;
    private ApiCookieProvider cookieProvider;
    private boolean debuggable;
    private Map<String, String> headers;
    private ArrayList<Interceptor> interceptors;
    private String mediaType;
    private long readTimeout;

    private ApiClientConfig() {
        this.interceptors = new ArrayList<>();
    }

    private ApiClientConfig(Builder builder) {
        this.interceptors = new ArrayList<>();
        this.readTimeout = builder.readTimeout;
        this.connectTimeout = builder.connectTimeout;
        this.cookieProvider = builder.cookieProvider;
        this.cacheDir = builder.cacheDir;
        this.cacheSize = builder.cacheSize;
        this.debuggable = builder.debuggable;
        this.headers = builder.headers;
        this.baseUrl = builder.baseUrl;
        this.mediaType = builder.mediaType;
        this.interceptors = builder.interceptors;
    }

    public long readTimeout() {
        return this.readTimeout;
    }

    public long connectTimeout() {
        return this.connectTimeout;
    }

    public ApiCookieProvider cookieProvider() {
        return this.cookieProvider;
    }

    public String cacheDir() {
        return this.cacheDir;
    }

    public long cacheSize() {
        return this.cacheSize;
    }

    public boolean debuggable() {
        return this.debuggable;
    }

    public Map<String, String> headers() {
        return this.headers;
    }

    public String mediaType() {
        return this.mediaType;
    }

    public String baseUrl() {
        return this.baseUrl;
    }

    public ArrayList<Interceptor> getInterceptors() {
        return this.interceptors;
    }

    public void setInterceptors(ArrayList<Interceptor> interceptors) {
        this.interceptors = interceptors;
    }

    public Builder newBuilder() {
        return new Builder();
    }

    public static class Builder {
        private String baseUrl;
        private String cacheDir;
        private long cacheSize;
        private long connectTimeout;
        private ApiCookieProvider cookieProvider;
        private boolean debuggable;
        private Map<String, String> headers;
        private ArrayList<Interceptor> interceptors;
        private String mediaType;
        private long readTimeout;

        public Builder() {
            this.headers = new HashMap();
            this.interceptors = new ArrayList<>();
        }

        private Builder(ApiClientConfig config) {
            this.headers = new HashMap();
            this.interceptors = new ArrayList<>();
            this.readTimeout = config.readTimeout;
            this.connectTimeout = config.connectTimeout;
            this.cookieProvider = config.cookieProvider;
            this.cacheDir = config.cacheDir;
            this.cacheSize = config.cacheSize;
            this.debuggable = config.debuggable;
            this.baseUrl = config.baseUrl;
            this.mediaType = config.mediaType;
        }

        public Builder readTimeout(long timeout, TimeUnit timeUnit) {
            this.readTimeout = timeUnit.toMillis(timeout);
            return this;
        }

        public Builder connectTimeout(long timeout, TimeUnit timeUnit) {
            this.connectTimeout = timeUnit.toMillis(timeout);
            return this;
        }

        public Builder cookieProvider(ApiCookieProvider cookieProvider) {
            this.cookieProvider = cookieProvider;
            return this;
        }

        public Builder addInterceptor(Interceptor interceptor) {
            this.interceptors.add(interceptor);
            return this;
        }

        public Builder cache(String cacheDir, long size) {
            this.cacheDir = cacheDir;
            this.cacheSize = size;
            return this;
        }

        public Builder baseUrl(String url) {
            this.baseUrl = url;
            return this;
        }

        public Builder mediaType(String mediaType) {
            this.mediaType = mediaType;
            return this;
        }

        public Builder debuggable(boolean debug) {
            this.debuggable = debug;
            return this;
        }

        public Builder header(String key, String value) {
            this.headers.put(key, value);
            return this;
        }

        public ApiClientConfig build() {
            return new ApiClientConfig(this);
        }
    }
}
